#include "LoadingScreen.h"

#include "SuperData.h"
#include "General Structures.hpp"
#include "GLPrimitives.h"
#include "GenericKeys.h"

// Initializes the loading screen, sets the bar position, and the bar outline.
LoadingScreen::LoadingScreen(string title, string subtitle, string informational)
{
	m_title = title;
	m_subtitle = subtitle;
	m_informational = informational;

	m_barOutline.x = 100;
	m_barOutline.y = DataLoader->WINDOW_HEIGHT - 50;
	m_barOutline.w = DataLoader->WINDOW_WIDTH - 200;
	m_barOutline.h = 45;
	
	m_barPos.x = 100;
	m_barPos.y = DataLoader->WINDOW_HEIGHT - 50;
	m_barPos.w = 0;
	m_barPos.h = 45;
}

// Renders the loading screen, and swaps the buffer.
void LoadingScreen::Render()
{	
	// background
	DrawPrimitives->DrawRectangle(0, 0, DataLoader->WINDOW_WIDTH, DataLoader->WINDOW_HEIGHT, Key->GetColor("145, 145, 145"));
	
	// loading screen
	DrawPrimitives->DrawRectangle(m_barOutline.x, m_barOutline.y, m_barOutline.x + m_barOutline.w, m_barOutline.y + m_barOutline.h, Key->GetColor("201, 201, 201"));
	DrawPrimitives->DrawRectangle(m_barPos.x + 2, m_barPos.y + 2, m_barPos.x + m_barPos.w - 2, m_barPos.y + m_barPos.h - 2, Key->GetColor("232, 232, 232"));
	DrawPrimitives->DrawRectangle(m_barPos.x + 5, m_barPos.y + 5, m_barPos.x + m_barPos.w - 5, m_barPos.y + m_barPos.h - 5, Key->GetColor("189, 189, 189"));

	// title text
	int mw = Key->GetFont("onuava big")->calcStringWidth(m_title);
	int mh = Key->GetFont("onuava big")->getHeight();
	int mx = (DataLoader->WINDOW_WIDTH / 2) - (mw / 2);
	int my = 50;
	DrawPrimitives->DrawRectangle(mx - 5, my - 5, mx + mw + 5, my + mh + 5, Key->GetColor("204, 204, 204"));
	DrawPrimitives->DrawRectangle(mx - 2, my - 2, mx + mw + 2, my + mh + 2, Key->GetColor("150, 150, 150"));
	Key->GetFont("onuava big")->drawText(mx, my, m_title, Key->GetColor("217, 217, 217"));

	// subtitle text
	int tw = Key->GetFont("self_destruct")->calcStringWidth(m_subtitle);
	int th = Key->GetFont("self_destruct")->getHeight();
	int tx = (DataLoader->WINDOW_WIDTH / 2) - (tw / 2);
	int ty = my + mh + 8;
	DrawPrimitives->DrawRectangle(tx - 5, ty - 5, tx + tw + 5, ty + th + 5, Key->GetColor("204, 204, 204"));
	DrawPrimitives->DrawRectangle(tx - 2, ty - 3, tx + tw + 2, ty + th + 2, Key->GetColor("150, 150, 150"));
	Key->GetFont("self_destruct")->drawText(tx, ty, m_subtitle, Key->GetColor("217, 217, 217"));


	
	// informational text
	int bw = Key->GetFont("onuava")->calcStringWidth(m_informational);
	int bh = Key->GetFont("onuava")->getHeight();
	int bx = m_barOutline.x + (m_barOutline.w / 2) - (bw / 2);
	int by = m_barOutline.y - (bh / 2);
	DrawPrimitives->DrawRectangle(bx - 2, by - 2, bx + bw + 2, by + bh + 2, Key->GetColor("122, 122, 122"));
	Key->GetFont("onuava")->drawText(bx, by, m_informational, Key->GetColor("217, 217, 217"));

	// Swap the buffer
	SDL_GL_SwapBuffers();
}

void LoadingScreen::SetProgress(double progress, string informational)
{
	m_barPos.w = m_barOutline.w * progress;

  if(strcmp(informational.c_str(), "__DoNotChange") != 0)
    m_informational = informational;
}